package com.example.service.impl;

import com.example.common.PageResult;
import com.example.dto.StudentPageDto;
import com.example.entity.Student;
import com.example.exception.CustomException;
import com.example.mapper.StudentMapper;
import com.example.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public List<Student> getStudent() {
        return studentMapper.getStudent();
    }

    @Override
    public PageResult getBySnameWithSno(StudentPageDto studentPageDto) {
        PageHelper.startPage(studentPageDto.getPageNum(), studentPageDto.getPageSize());
        Page<Student> page = studentMapper.getBySnameWithSnoWithDno(studentPageDto);
        long total = page.getTotal();
        List<Student> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void add(Student student) {
        int count = studentMapper.getBySno(student);
        if(count > 0){
//            studentMapper.deleteBySno(student.getSno());
            throw new CustomException("已有该学生信息");
        }
        studentMapper.insert(student);
    }

    @Override
    public void deleteBySno(String sno) {
        studentMapper.deleteBySno(sno);
    }

    @Override
    public void update(Student student) {
        studentMapper.update(student);
    }
}
